package it.trekker.upload.xml;

import it.trekker.db.Excursion;
import it.trekker.db.ExcursionCoordinate;
import it.trekker.db.GPSCoordinate;
import it.trekker.db.Poi;
import it.trekker.upload.User;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;

public class XmlExporter implements IXmlExporter {
	
	private XmlSerializer serializer;
	private FileOutputStream fos;
	
	private static final String EXCURSIONS_TAG = "excursions";
	private static final String EXCURSION_TAG = "excursion";
	private static final String USER_TAG = "user";
	private static final String USERNAME_ATTRIBUTE = "username";
	private static final String COORDINATES_TAG = "coordinates";
	private static final String COORDINATE_TAG = "coordinate";
	private static final String POIS_TAG = "pois";
	private static final String POI_TAG = "poi";
	private static final String NAME_ATTRIBUTE = "name";
	private static final String DATE_ATTRIBUTE = "date";
	private static final String DISTANCE_TAG = "distance";
	private static final String ALTDIFF_TAG = "altitudeDifference";
	private static final String TIME_TAG = "time";
	private static final String MEANPACE_TAG = "meanPace";
	private static final String STEPS_TAG = "steps";
	private static final String LATITUDE_TAG = "latitude";
	private static final String LONGITUDE_TAG = "longitude";
	private static final String ALTITUDE_TAG = "altitude";
	
	public XmlExporter() throws IllegalArgumentException, IllegalStateException, IOException {
		serializer = Xml.newSerializer();
	}
	
	@Override
	public String startDocumentXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startDocument("UTF-8", true);
		
		return writer.toString();
	}
	
	@Override
	public String endDocumentXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endDocument();
		
		return writer.toString();
	}
	
	
	@Override
	public String openExcursionsXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startTag("", EXCURSIONS_TAG);
		
		return writer.toString();
	}
	
	
	@Override
	public String closeExcursionsXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endTag("", EXCURSIONS_TAG);
		
		return writer.toString();
	}
	

	@Override
	public String openExcursionXml(Excursion excursion) throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startTag("", EXCURSION_TAG);
		serializer.attribute("", NAME_ATTRIBUTE, excursion.getName());
		serializer.attribute("", DATE_ATTRIBUTE, excursion.getDate().toString());
		serializer.startTag("", DISTANCE_TAG);
		serializer.text(String.valueOf(excursion.getDistance()));
		serializer.endTag("", DISTANCE_TAG);
		serializer.startTag("", ALTDIFF_TAG);
		serializer.text(String.valueOf(excursion.getTotAltitudeDifference()));
		serializer.endTag("", ALTDIFF_TAG);
		serializer.startTag("", TIME_TAG);
		serializer.text(String.valueOf(excursion.getTime()));
		serializer.endTag("", TIME_TAG);
		serializer.startTag("", MEANPACE_TAG);
		serializer.text(String.valueOf(excursion.getMeanPace()));
		serializer.endTag("", MEANPACE_TAG);
		serializer.startTag("", STEPS_TAG);
		serializer.text(String.valueOf(excursion.getSteps()));
		serializer.endTag("", STEPS_TAG);
		
		return writer.toString();
	}

	@Override
	public String closeExcursionXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endTag("", EXCURSION_TAG);
		
		return writer.toString();
	}
	
	
	@Override
	public String openUserXml(User user) throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startTag("", USER_TAG);
		serializer.attribute("", USERNAME_ATTRIBUTE, user.getUsername());
		
		return writer.toString();
	}

	@Override
	public String closeUserXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endTag("", USERNAME_ATTRIBUTE);
		
		return writer.toString();
	}
	
	
	@Override
	public String openExcursionCoordinatesXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startTag("", COORDINATES_TAG);
		
		return writer.toString();
	}
	
	
	@Override
	public String closeExcursionCoordinatesXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endTag("", COORDINATES_TAG);
		
		return writer.toString();
	}
	

	@Override
	public String openExcursionCoordinateXml(ExcursionCoordinate coordinate) throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startTag("", COORDINATE_TAG);
		serializer.attribute("", DATE_ATTRIBUTE, coordinate.getTimestamp().toString());
		writeGPSCoordinate(coordinate.getCoordinate());
		
		return writer.toString();
	}

	@Override
	public String closeExcursionCoordinateXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endTag("", COORDINATE_TAG);
		
		return writer.toString();
	}
	
	
	@Override
	public String openPoisXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startTag("", POIS_TAG);
		
		return writer.toString();
	}
	
	
	@Override
	public String closePoisXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endTag("", POIS_TAG);
		
		return writer.toString();
	}
	

	@Override
	public String openPoiXml(Poi poi) throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.startTag("", POI_TAG);
		serializer.attribute("", NAME_ATTRIBUTE, poi.getFriendlyName());
		writeGPSCoordinate(poi.getLocation());
		
		return serializer.toString();
	}

	@Override
	public String closePoiXml() throws IllegalArgumentException, IllegalStateException, IOException {
		StringWriter writer = new StringWriter();
		serializer.setOutput(writer);
		
		serializer.endTag("", POI_TAG);
		
		return writer.toString();
	}

	
	private void writeGPSCoordinate(GPSCoordinate coordinate) throws IllegalArgumentException, IllegalStateException, IOException {
		serializer.startTag("", LATITUDE_TAG);
		serializer.text(String.valueOf(coordinate.getLatitude()));
		serializer.endTag("", LATITUDE_TAG);
		serializer.startTag("", LONGITUDE_TAG);
		serializer.text(String.valueOf(coordinate.getLongitude()));
		serializer.endTag("", LONGITUDE_TAG);
		serializer.startTag("", ALTITUDE_TAG);
		serializer.text(String.valueOf(coordinate.getAltitude()));
		serializer.endTag("", ALTITUDE_TAG);
	} 
	
}
